﻿@using TS2015.Models;
@{
    AjaxOptions aj = new AjaxOptions
    {
        InsertionMode = InsertionMode.Replace,
        LoadingElementId = "loading-icon",
        UpdateTargetId = "content-main",
        OnFailure = "AjaxFailure",
        OnBegin = "AjaxBegin",
        OnComplete = "AjaxComplete"
    };

    if (Session["Ad_TenDangNhap"] == null || Session["Roles"] == null)
    {
        Response.Redirect("~/Admin/Home/Login");
    }

    TS2015Context db = new TS2015Context();
    var roles = Session["Roles"] as int[];
    IEnumerable<TbChucNang> TbRoles = db.TbChucNangs.Where(m => roles.Contains(m.MaCN));
}
<aside class="sidebar offscreen-left">

    <nav id="main-navigation" class="main-navigation" data-height="auto" data-size="6px" data-distance="0" data-rail-visible="true" data-wheel-step="10">

        <ul class="nav mb20"> 
            <li>
                <a href="/Home" target="_blank">
                    <i class="glyphicon glyphicon-globe"></i>
                    <span>Trang chủ</span>
                </a>
            </li>

            @RenderMenuItem(TbRoles, 0, aj)

            @helper RenderMenuItem(IEnumerable<TbChucNang> TbRoles, int role, AjaxOptions aj)
            
            {
                var list_role = TbRoles.Where(m => m.MaCNCha == role && m.TrangThai == 1 && m.IsMenu == true).OrderBy(m=>m.ThuTu);
                foreach (var item in list_role)
                {
                    int count_child = TbRoles.Where(m => m.MaCNCha == item.MaCN && m.TrangThai == 1 && m.IsMenu == true).Count();

                    <li>
                        @if (count_child > 0)
                        {
                            <a href="javascript:;">
                                <i class="toggle-accordion glyphicon glyphicon-chevron-down"></i>
                                <i class="glyphicon @item.CssIcon"></i>
                                <span>@item.TenCN</span>
                            </a>

                            <ul class="sub-menu">
                                @RenderMenuItem(TbRoles, item.MaCN, aj)
                            </ul>
                        }
                        else
                        {
                            string[] Link = item.Link.TrimEnd().Split('/');
                            string Controller = Link[0].ToString().TrimEnd();
                            string Action = "";
                            if (Link.Count() > 1)
                            {
                                Action = Link[1].ToString().TrimEnd();
                            }
                            if (Action != "")
                            {
                                @Ajax.ActionLink(item.TenCN, Action, Controller, aj);
                            }
                        }
                    </li>
                }
            }
        </ul>
    </nav>
</aside>